﻿using System;
using System.Data;
using ChapterX.BusinessFacade;
using ChapterX.Common;
using ChapterX.Data.BooksDSTableAdapters;

namespace ChapterX.Modules
{
    public partial class BookCourseListModule : ChapterX.Model.DataListModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindBookCourseList();
            }
        }

        public void BindBookCourseList()
        {
            int bookId;

            if (Int32.TryParse(Request.QueryString[QueryKeys.BOOK_ID], out bookId))
            {
                try
                {
                    CourseBookViewTableAdapter adpater = new CourseBookViewTableAdapter();
                    if (CurrentUserInfo.IsLibrarian)
                    {
                        BookCourseList.DataSource = adpater.GetCourseListByBookId(bookId);
                    }
                    else
                    {
                        BookCourseList.DataSource = adpater.GetCourseListByBookIdAndTeacherId(bookId, CurrentUserInfo.Username);
                    }
                    BookCourseList.DataBind();
                }
                catch (Exception ex)
                {
                    SetErrorMessage("数据绑定失败。");
                    Logger.Log(ex);
                }
            }
        }
    }
}